﻿using SharpAdbClient;
using SharpAdbClient.DeviceCommands;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DroidLord.Core
{
    class AdbManager
    {
        public static string adbPath = null;
        public static void initAdbPath(string adbpath)
        {
            adbPath = adbpath;
        }


        public static void killServer()
        {
            ExecCmd(adbPath + " kill-server");
        }

        public static void startServer()
        {
            ExecCmd(adbPath + " start-server");
        }

        public  static string ExecCmd(string cmd)
        {
            Process MyProcess = new Process();
            MyProcess.StartInfo.FileName = "cmd.exe";
            MyProcess.StartInfo.UseShellExecute = false;
            MyProcess.StartInfo.RedirectStandardInput = true;
            MyProcess.StartInfo.RedirectStandardOutput = true;
            MyProcess.StartInfo.RedirectStandardError = true;
            MyProcess.StartInfo.CreateNoWindow = true;
            MyProcess.Start();
            MyProcess.StandardInput.WriteLine(cmd);
            MyProcess.StandardInput.WriteLine("exit");
            String StrInfo = MyProcess.StandardOutput.ReadToEnd();
            return StrInfo;
        }





        public static  void InstallApk(DeviceData device, String apkPath)
        {
            try
            {
                var adb = new AdbClient();

                PackageManager manager = new PackageManager(device,false,adb);
                manager.InstallPackage(apkPath, reinstall: true);
            }catch(Exception EEEE)
            {
                // MessageBox.Show(EEEE.ToString());
                adbInstall(device.Serial, apkPath);
            }

        }

        public static void adbInstall(String sn,String apkPath)
        {
            String cmdString = adbPath + " -s " + sn + " install -r " + apkPath;
            ExecCmd(cmdString);
        }

        public static string executeShell(DeviceData deviceData,string shellCmd)
        {
            try
            {
                var receiver = new ConsoleOutputReceiver();
                var adb = new SharpAdbClient.AdbClient();
                adb.ExecuteRemoteCommand(shellCmd, deviceData, receiver);
                //adb.ExecuteShellCommand(deviceData, shellCmd, receiver);
                var retMsg = receiver.ToString();
                return retMsg;
            }catch(Exception eeee)
            {

            }
            return "";
        }

        public static void  executeShellNoRead(DeviceData deviceData, string shellCmd)
        {
            try
            {

                var adb = new SharpAdbClient.AdbClient();
                adb.ExecuteRemoteCommand(shellCmd, deviceData, null);
            }catch(Exception eeee)
            {

            }
            
        }

        public static void adbShellNoRead(DeviceData deviceData,string shellCmd)
        {
            string adbcmd = adbPath + " -s " + deviceData.Serial + " shell " + shellCmd;
            //MessageBox.Show(adbcmd);
            ExecCmd(adbcmd);
        }


        public static void  adbPull(DeviceData deviceData,String phonePath,String pcPath)
        {
            string cmd = adbPath + " -s " + deviceData.Serial + " pull " + phonePath + " " + pcPath;
            ExecCmd(cmd);
        }

        public static List<String> GetInstalledPackages(DeviceData deviceData)
        {
            
            List<String> mylist = new List<String>();
            try
            {
                var receiver = new ConsoleOutputReceiver();
                var adb = new SharpAdbClient.AdbClient();
                // adb.ExecuteRemoteCommand("pm list packages -3", deviceData, receiver);
                var retMsg = executeShell(deviceData, "pm list packages -3");
                //MessageBox.Show(retMsg);
                string[] msgs = retMsg.Split(new char[] { '\r', '\n' });
                if (msgs != null)
                {
                    foreach (string line in msgs)
                    {
                        string myline = line.Trim();
                        if (myline.StartsWith("package:"))
                        {
                            mylist.Add(myline.Replace("package:", "").Trim());
                        }
                    }
                }
            }catch(Exception eeee)
            {

            }
            return mylist;
        }


        public static string  getAppVersionName(DeviceData deviceData,string pkgName)
        {
            string versionName = "";
            try
            {
                var receiver = new ConsoleOutputReceiver();
                var adb = new SharpAdbClient.AdbClient();
                var retMsg = executeShell(deviceData, "pm dump "+pkgName);
                //MessageBox.Show(retMsg);
                string[] msgs = retMsg.Split(new char[] { '\r', '\n' });
                if (msgs != null)
                {
                    foreach (string line in msgs)
                    {
                        string myline = line.Replace(" ","").Trim();
                        if (myline.StartsWith("versionName="))
                        {
                            versionName=myline.Replace("versionName=", "").Trim();
                            break;
                        }
                    }
                }
            }
            catch (Exception eeee)
            {

            }
            return versionName;
        }




    }
}
